﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using TEL.ClientClasses;
using TEL.ClientClasses.SipInterfaces;
using System.Windows.Forms;

namespace TEL.TelPhoneF.Controls
{
    public partial class BtnPanel : CustomPanel, IBtnPannel
    {
        private List<ButtonNumber> btns;
        public BtnPanel()
        {
            InitializeComponent();
            Paths paths = new Paths();

            this.Region = new Region(paths.BtnPanelPath(new Point(0, 0), this.Size));

            btn0.KeyDown += KeyDownEvent;
            btn1.KeyDown += KeyDownEvent;
            btn2.KeyDown += KeyDownEvent;
            btn3.KeyDown += KeyDownEvent;
            btn4.KeyDown += KeyDownEvent;
            btn5.KeyDown += KeyDownEvent;
            btn6.KeyDown += KeyDownEvent;
            btn7.KeyDown += KeyDownEvent;
            btn8.KeyDown += KeyDownEvent;
            btn9.KeyDown += KeyDownEvent;
            btn10.KeyDown += KeyDownEvent;
            btn11.KeyDown += KeyDownEvent;
            btns = (from q in this.Controls.Cast<Control>() where q is ButtonNumber select q as ButtonNumber).ToList();
        }

        #region Implementation of IBtnPannel

        public event EventHandler<ClickNumberEventArgs> NumberEvent;

        private string _callLine;
        public string CallLine
        {
            get { return _callLine; }

        }

        public void SetEnabled(bool value)
        {
            try
            {

                this.Invoke(new MethodInvoker(delegate()
                                                       {
                                                           this.Enabled = value;
                                                       }), null);

            }
            catch (Exception)
            {

                throw;
            }
        }

        public void SetCallLine(string value)
        {
            try
            {
                this.Invoke(new MethodInvoker(delegate()
                                                       {
                                                           _callLine = value;
                                                           //btnUp.Enabled = !string.IsNullOrEmpty(_callLine);
                                                           //BtnDoun.Enabled = !string.IsNullOrEmpty(_callLine);
                                                       }), null);

            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        private void Number_Click(object sender, EventArgs e)
        {
            if (sender is ButtonNumber)
                try
                {
                    ButtonNumber btn = (ButtonNumber)sender;
                    if (NumberEvent != null) NumberEvent(this, new ClickNumberEventArgs(btn.ID, btn.Cap, btn.RegexStr));
                }
                catch (Exception)
                {

                }
        }

        public void EnterNumber(string nuumber)
        {
           if(btns == null ) return;
            try
            {
                foreach (char item in nuumber)
                {
                    (from q in btns where q.Cap == item select q).First().PerformClick();
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
               
            }
           
        }

        public bool BtnPanel_KeyDown(KeyEventArgs e)
        {
            bool res = false;
            if (e.Alt || e.Control || e.Shift) return false;
            switch (e.KeyCode)
            {
                case Keys.D0:
                    btn0.PerformClick();
                    res = true;
                    break;
                case Keys.D1:
                    btn1.PerformClick();
                    res = true;
                    break;
                case Keys.D2:
                    btn2.PerformClick();
                    res = true;
                    break;
                case Keys.D3:
                    btn3.PerformClick();
                    res = true;
                    break;
                case Keys.D4:
                    btn4.PerformClick();
                    res = true;
                    break;
                case Keys.D5:
                    btn5.PerformClick();
                    res = true;
                    break;
                case Keys.D6:
                    btn6.PerformClick();
                    res = true;
                    break;
                case Keys.D7:
                    btn7.PerformClick();
                    res = true;
                    break;
                case Keys.D8:
                    btn8.PerformClick();
                    res = true;
                    break;
                case Keys.D9:
                    btn9.PerformClick();
                    res = true;
                    break;

                case Keys.NumPad0:
                    btn0.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad1:
                    btn1.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad2:
                    btn2.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad3:
                    btn3.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad4:
                    btn4.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad5:
                    btn5.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad6:
                    btn6.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad7:
                    btn7.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad8:
                    btn8.PerformClick();
                    res = true;
                    break;
                case Keys.NumPad9:
                    btn9.PerformClick();
                    res = true;
                    break;
            }
            return res;
        }

        private void BtnPanel_Load(object sender, EventArgs e)
        {

        }

    }
}